# Defaults

from random import randrange
NSOName = "nso%d" % (randrange(1, 100000))

BoutName = ""

SendTimers = False

import DerbyXML
InitialDerbyXML = DerbyXML.DerbyXML.defaultBout()

# we also set up the logging
import logging


loggingNames = [
    "KVO",
    "DNS",
    "MSG",
    "MSG.SEND",
    "MSG.RECV",
    "PEER",
    "XML",
    "XML.SET",
    "XML.ADD",
    "XML.DEL",
    "XML.REPL",
    "XML.GSIP",
    "NSO",
    "NSO.XML"
]
    

# To parse options from the command line, either just call Defaults.ParseOptions, or else:
#  parser = Defaults.OptionsParser()
#  ... configure parser for additional options
#  (options, args) = parser.parse_args()
#  Defaults.ConfigureOptions(options)

def OptionsParser():
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-n","--nso",
                      action="store", type="string", dest="nsoname",
                      help="NSO name")
    parser.add_option("-b","--bout",
                      action="store", type="string", dest="boutname",
                      help="Name of bout")
    parser.add_option("-x","--xml",
                      action="store", type="string", dest="xmlpath",
                      help="Default DerbyXML file")
    parser.add_option("--home",
                      action="store", type="string", dest="homepath",
                      help="Path to home team DerbyXML <team> file")
    parser.add_option("--away",
                      action="store", type="string", dest="awaypath",
                      help="Path to away team DerbyXML <team> file")
    parser.add_option("--officials",
                      action="store", type="string", dest="officialspath",
                      help="Path to officials DerbyXML <team> file")
    parser.set_defaults(timer=False)
    parser.add_option("--timer", action="store_true", dest="timer",
                      help="Send <timer> events")
    parser.add_option("--notimer", action="store_false", dest="timer",
                      help="Disable sending <timer> events")
    parser.add_option("-q","--quiet",
                      action="store_true", dest="quiet",
                      help="Suppress all status messages")
    parser.add_option("--log",
                      action="store",type="string", dest="log", default="INFO",
                      help="Set logging level of all logs")
    parser.add_option("-v","--verbose",
                      action="append", type="string", dest="verbose",
                      help="""Add verbose debug logging for a module:
    KVO - key value observing
    DNS - DNS discovery
    MSG - Messages
    MSG.SEND - Sending messages
    MSG.RECV - Receiving messages
    PEER - Peer negotiation
    XML - DerbyXML actions
    XML.SET - XML set messages
    XML.ADD - XML add messages    
    XML.DEL - XML delete messages
    XML.REPL - XML replace messages
    XML.GSIP - XML gossip sync messages
    NSO - NSOde messages
    NSO.XML - dump current bout xml
""")
    return parser

def TeamOptionsParser():
    parser = OptionsParser()
    return parser

def ConfigureOptions(opts):
    import sys
    if opts.nsoname:
        global NSOName
        NSOName = opts.nsoname
        
    if opts.boutname:
        global BoutName
        BoutName = opts.boutname
    defaultLogLevel = logging.INFO
    uverbose = []
    if opts.verbose:
        for name in opts.verbose:
            uverbose.append(name.upper())
    # for now, while debugging GSIP
    uverbose.append("XML.GSIP")
    if opts.quiet:
        defaultLogLevel = logging.WARNING
    elif opts.log:
        defaultLogLevel = opts.log.upper()
    for name in loggingNames:
        log = logging.getLogger(name)
        if name in uverbose:
            log.setLevel(logging.DEBUG)
        else:
            log.setLevel(defaultLogLevel)
        log.addHandler(logging.StreamHandler(sys.stdout))
    if opts.timer:
        global SendTimers
        SendTimers = True
    global InitialDerbyXML
    if opts.xmlpath:
        InitialDerbyXML = DerbyXML.DerbyXML.parse(opts.xmlpath)
    if opts.homepath:
        home = DerbyXML.DerbyXML.parse(opts.homepath)
        InitialDerbyXML.home = home
    if opts.awaypath:
        away = DerbyXML.DerbyXML.parse(opts.awaypath)
        InitialDerbyXML.away = away
    if opts.officialspath:
        officials = DerbyXML.DerbyXML.parse(opts.officialspath)
        InitialDerbyXML.officials = officials
    
def ParseOptions():
    parser = OptionsParser()
    (options, args) = parser.parse_args()
    ConfigureOptions(options)
    parser.destroy()
